﻿using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace SharePointOfView.Utilities
{
    /// <summary>
    /// Localization helper class : get localized content from resource key for a specific LCID
    /// </summary>
    public class Localization
    {
        /// <summary>
        /// Default resource file (core is the default WSS resource file)
        /// </summary>
        private const string defaultResourceFile = "core";
        /// <summary>
        /// Resource internal prefix
        /// </summary>
        private const string resourcesPrefix = "$Resources:";
        /// <summary>
        /// Resource file
        /// </summary>
        private readonly string resFile = defaultResourceFile;
        /// <summary>
        /// LCID
        /// </summary>
        private readonly uint lcid = 0;

        #region Constructors
        /// <summary>
        /// Use current website's language and "core" as default resource file 
        /// </summary>
        public Localization()
        {
            lcid = SPContext.Current.Web.Language;
        }

        /// <summary>
        /// Use current website's language and a specific resource file 
        /// </summary>
        /// <param name="defaultResourceFile">Default Resource filename</param>
        public Localization(string defaultResourceFile)
        {
            resFile = defaultResourceFile;
            lcid = SPContext.Current.Web.Language;
        }

        /// <summary>
        /// Use a specific language and the default "core" resource file 
        /// </summary>
        /// <param name="lcid">Culture ID</param>
        public Localization(uint lcid)
        {
            this.lcid = lcid;
        }

        /// <summary>
        /// Use specific language and resource file 
        /// </summary>
        /// <param name="defaultResourceFile">Default Resource filename</param>
        /// <param name="lcid">Culture ID</param>
        public Localization(string defaultResourceFile, uint lcid)
        {
            resFile = defaultResourceFile;
            this.lcid = lcid;
        }
        #endregion

        /// <summary>
        /// Get string from resource key
        /// </summary>
        /// <param name="resKey">Resource key</param>
        /// <returns></returns>
        public string GetResource(string resKey)
        {
            if (resFile != null)
                return GetResource(resKey, resFile, lcid);
            return resKey;
        }

        /// <summary>
        /// Get string from key, resource file and language
        /// </summary>
        /// <param name="resKey">Resource key</param>
        /// <param name="resFile">Resource file ("core" for core.resx, core.en-US.resx, etc)</param>
        /// <param name="lcid">LCID</param>
        /// <returns></returns>
        public static string GetResource(string resKey, string resFile, uint lcid)
        {
            string resValue = SPUtility.GetLocalizedString(resourcesPrefix + resKey, resFile, lcid);
            if (resValue == resourcesPrefix + resKey && lcid != 0)
            {
                resValue = SPUtility.GetLocalizedString(resourcesPrefix + resKey, resFile, 0);
            }

            return resValue;
        }
    }
}
